﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Models;
using WebHoiChoMuaBan.Controllers.Helpers;
using System.Drawing;
using WebHoiChoMuaBan.Controllers.Utility;
using System.IO;

namespace WebHoiChoMuaBan.Controllers.Private
{
    public class ProfileController : PrivateController
    {
        private void Init()
        {
            ViewData["mid"] = "edit_profile";
            LoadTimeAndCurrency();
            //UtilityModel uModel = new UtilityModel();
            //ViewData["Provinces"] = new SelectList(uModel.ListProvince(),"ProvId","ProvName");
        }

        public ActionResult EditProfile(int? uid)
        {
            if (uid == null)
                return HomePage();
            if (LoadMember(uid.Value) == null)
            {
                Init();
                MemberHelper mHelper = new MemberHelper();
                Member mem = mHelper.getMember(uid.Value);
                UtilityModel uModel = new UtilityModel();
                ViewData["Provinces"] = new SelectList(uModel.ListProvince(), "ProvId", "ProvName",mem.ProId);
                ViewData["Member"] = mem;
                return View("~/Views/Private/EditProfile.aspx");
            }
            return HomePage();
        }

        public ActionResult UpdateProfile(FormCollection data,HttpPostedFileBase banner, HttpPostedFileBase background, HttpPostedFileBase avatar)
        {
            int? uid = null;
            if (Session["MemId"] != null)
                uid = (int)Session["MemId"];
            if (!this.IsLogin || uid == null)
                return HomePage();
            if (LoadMember(uid.Value) == null)
            {
                MemberHelper memHelper = new MemberHelper();
                Member mem = memHelper.getMember(uid.Value);
                mem.MemFullname = data.Get("fullname");
                if(data.Get("birthday").Equals("Năm/Tháng/Ngày"))
                    mem.MemBirthdate = null;
                else
                    mem.MemBirthdate = DateTime.Parse(data.Get("birthday"));
                mem.MemAddress = data.Get("address");
                mem.ProId = int.Parse(data.Get("ddlProvince"));
                mem.MemCompany = data.Get("compName");
                mem.MemCompanyIntro = data.Get("introComp");
                mem.MemSelfIntro = data.Get("selfIntro");
                mem.MemWebSite = data.Get("website");
                mem.MemHomePhone = data.Get("phone");
                mem.MemCellPhone = data.Get("cellphone");
                mem.MemJob = data.Get("job");
                mem.MemFax = data.Get("fax");
                mem.MemBanner = data.Get("banner");
                mem.MemBgImg = data.Get("background");
                mem.MemAvatar = data.Get("avatar");
                //Lưu hình banner
                if (banner != null)
                {
                    if (banner.ContentLength != 0)
                    {
                        string fileName = banner.FileName;
                        mem.MemBanner = fileName;
                        //Up hình lên server
                        Bitmap img_banner = new Bitmap(banner.InputStream);
                        var path = Path.Combine(Server.MapPath("~/Content/Images/Upload"), fileName);
                        img_banner.Save(path);
                        img_banner.Dispose();
                    }
                }
                //else mem.MemBanner = "default-banner.jpg";
                
                //Lưu hình background
                if (background != null)
                {
                    if (background.ContentLength != 0)
                    {
                        string fileName = banner.FileName;
                        mem.MemBgImg = fileName;
                        //Up hình lên server
                        Bitmap image = new Bitmap(background.InputStream);
                        var path = Path.Combine(Server.MapPath("~/Content/Images/Upload"), fileName);
                        image.Save(path);
                        image.Dispose();
                    }
                }
               // else mem.MemBgImg = "default-banner.jpg";

                //Lưu hình avatar
                if (avatar != null)
                {
                    if (avatar.ContentLength != 0)
                    {
                        string fileName = avatar.FileName;
                        mem.MemAvatar = fileName;
                        //Up hình lên server
                        Bitmap image = new Bitmap(avatar.InputStream);
                        var path = Path.Combine(Server.MapPath("~/Content/Images/Upload"), fileName);
                        image.Save(path);
                        image.Dispose();
                    }
                }
                //else
                //{
                //    mem.MemAvatar = "default-avatar.jpg";   
                //}
                memHelper.Save();
                return EditProfile(uid);
            }
            return HomePage();
        }
    }
}
